package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.FeedBackDao;
import com.leoman.entity.FeedBack;
import com.leoman.service.FeedBackService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * FeedBackServiceImpl
 * Created by 涂奕恒 on 2016/11/23 0023 下午 3:58.
 */
@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private FeedBackDao feedBackDao;

    @Override
    public List<FeedBack> findAll() {
        return feedBackDao.findAll();
    }

    @Override
    public Page<FeedBack> find(int pageNum, int pageSize) {
        return feedBackDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<FeedBack> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public FeedBack getById(int id) {
        return feedBackDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        feedBackDao.delete(id);
    }

    @Override
    public FeedBack create(FeedBack feedBack) {
        return feedBackDao.save(feedBack);
    }

    @Override
    public FeedBack update(FeedBack feedBack) {
        return feedBackDao.save(feedBack);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<FeedBack> pageByParams(final String mobile, final String startTime, final String endTime, Integer pageNum, Integer pageSize) {
        return feedBackDao.findAll(new Specification<FeedBack>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<FeedBack> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (StringUtils.isNotBlank(mobile)) {
                    Predicate pre = cb.like(root.get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotBlank(startTime)) {
                    try {
                        Long startDate = DateUtils.stringToDateWithFormat(startTime, "yyyy-MM-dd hh:mm:ss").getTime();
                        predicateList.add(cb.gt(root.get("createTime").as(Long.class), startDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (StringUtils.isNotBlank(endTime)) {
                    try {
                        Long endDate = DateUtils.stringToDateWithFormat(endTime, "yyyy-MM-dd hh:mm:ss").getTime();
                        predicateList.add(cb.lt(root.get("createTime").as(Long.class), endDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    @Transactional
    public Integer batchDelete(String ids) {
        try {
            Integer[] feedBackIds = JsonUtil.json2Obj(ids, Integer[].class);

            for (Integer feedBackId : feedBackIds) {
                feedBackDao.delete(feedBackId);
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer delete(Integer feedBackId) {
        try {
            feedBackDao.delete(feedBackId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}